/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.clicarros.dao.Implementacao;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import br.com.clicarros.util.UtilHibernate;

/**
 *
 * @author Bruno
 */
public abstract class AbstractDAO implements IDAO {

    @Override
    public Object inserir(Object o) throws ClassNotFoundException, SQLException, Exception {
        
        Session session = UtilHibernate.openSession();
		
	Transaction tz = session.beginTransaction();
				
	session.save(o);
	tz.commit();
	session.close();
        
        return o;
    }

    @Override
    public void alterar(Object o) throws ClassNotFoundException, SQLException, Exception {
        Session session = UtilHibernate.openSession();
		
	Transaction tz = session.beginTransaction();
				
	session.update(o);
	tz.commit();
	session.close();
    }

    @Override
    public void excluir(Object o) throws ClassNotFoundException, SQLException, Exception {
        Session session = UtilHibernate.openSession();
		
	Transaction tz = session.beginTransaction();
				
	session.delete(o);
	tz.commit();
	session.close();
    }

    @Override
    public List listarTodos(Object o) throws ClassNotFoundException, SQLException, Exception {
        
        Session session = UtilHibernate.openSession();
	
        List<Object> retorno;
       
        Criteria criterio = session.createCriteria(o.getClass());  


        retorno = criterio.list(); 
        
        session.close();

        return retorno;
        
    }


    @Override
    public Object consultarId(Object o) throws ClassNotFoundException, SQLException, Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
